<template>
    <div>
        <div class="e-form">
            <div class="tabs" style="margin-bottom: 120px">
                <div class="tabs-title">基本信息</div>
                <div style="width: 100%" class="form">
                    <el-form :model="basicForm" :label-width="formLabelWidth"
                     ref="ruleForm"
                     :rules="rules"
                     class="demo-ruleForm"
                    >
                        <el-row>

                            <el-col :span="12">
                                <el-form-item label="单据机构：">
                                    <span>{{ basicForm.orgName }}</span>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="采购方式："
                                prop="purchaseType">
                                    <el-radio-group
                                        v-model="basicForm.purchaseType"
                                    >
                                        <el-radio :label="0">普通采购</el-radio>
                                        <el-radio :label="1">集中采购</el-radio>
                                    </el-radio-group>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="计划日期："
                                prop="planDate"
                                >
                                    <el-date-picker
                                        value-format="yyyy-MM-dd"
                                        :clearable="false"
                                        type="date"
                                        placeholder="选择日期"
                                        v-model="basicForm.planDate"
                                    ></el-date-picker>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="计划年度："
                                prop="year"

                                >
                                    <el-date-picker
                                        value-format="yyyy"
                                        type="year"
                                        placeholder="选择年份"
                                        v-model="basicForm.year"
                                    ></el-date-picker>

                                </el-form-item>
                            </el-col>
                              <el-col :span="12">
                                <el-form-item label="填报人："
                                >
                                    <span>{{ basicForm.preparer }}</span>
                                </el-form-item>
                            </el-col>
                        </el-row>

                        <el-row>

                             <el-col :span="12">
                                  <el-form-item label="币种：" prop="currencyId">
                            <el-select
                                v-model="basicForm.currencyId"
                                placeholder="请选择"
                                @change="currencyChange"
                            >
                                <el-option
                                    v-for="(
                                        item
                                    ) in currencyForm"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                >
                                </el-option>
                            </el-select>
                        </el-form-item>
                            </el-col>
    <el-col :span="12">
                                <el-form-item label="人民币汇率：">
                                    <span>{{ basicForm.rmbRate }}</span>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>

                        </el-row>

                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="本位币：">
                                    <span>{{ basicForm.baseCurName }}</span>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <!-- <el-form-item label="调拨金额(本位币)：">
                                    <span>{{
                                        basicForm.transportationCost
                                    }}</span>
                                </el-form-item> -->
                            </el-col>
                        </el-row>
                        <el-row>

                            <el-col :span="12"> </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="本位币汇率："  v-if="showBaseCurName" prop="baseCurRate">
                                    <el-input v-enter-float1="{set:this,val:basicForm,key:baseCurRate}" v-model="basicForm.baseCurRate"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12"> </el-col>
                        </el-row>
                         <el-row>

                            <el-col :span="12">
                            </el-col>
                        </el-row>
                    </el-form>
                </div>
            </div>
            <div class="buttons">
                 <span class="tip"
        ><span style="color: red; margin-right: 0">*</span>为必填项</span
      >
                <el-button
                    type="primary"
                    size="small"
                    style="background: #2e61d7"
                    @click="addSave"
                    >保存</el-button>
                <el-button size="small" @click="handleClose">取消</el-button>
            </div>
        </div>
    </div>
</template>

<script>
import { mapState } from 'vuex'
import { getRmbRate } from '@/api/equip/common.js'

export default {

    data () {
        return {
            //基础信息
            basicForm: {
                'amount': 0,
                'amountExcludingTax': 0,
                'baseCurAmount': 0,
                'baseCurId': '112211',
                'baseCurName': '人民币',
                'baseCurRate': 0,
                'billId': '',
                'billNo': '',
                'currency': '',
                'currencyId': '',
                'nullifyCreated': '',
                'nullifyCreator': '',
                'nullifyCreatorId': '',
                'nullifyDescription': '',
                'nullifyReason': '',
                'orgId': '',
                'orgName': '',
                'period': 0,
                'planDate': new Date(),
                'preparer': '',
                'preparerId': '',
                'purchaseType': 0,
                'remarks': '',
                'rmbAmount': 0,
                'rmbId': '',
                'rmbName': '',
                'rmbRate': 0,
                'state': '',
                'taxAmount': 0,
                'taxRate': 0,
                'workId': '',
                'year': ''
            },
            formLabelWidth: '180px',
            currencyArr: [], //币种列表
            stateArr: ['草稿', '审核中', '已审核', '已作废'],
            rules: {
                planDate: [
                    { required: true, message: '请输入计划日期', trigger: 'blur' },
                ],
                year: [
                    { required: true, message: '请选择年份（元）', trigger: 'blur' },
                ],
                currencyId: [
                    { required: true, message: '请选择币种', trigger: 'change' },
                ],
                baseCurRate: [
                    { required: true, message: '请填写本位币汇率', trigger: 'change' },
                ],
            },
            showBaseCurName: false
        }
    },
    created () {
        this.getUserInfo()

    },
    computed: {

        ...mapState({
            userInfo: state => state.userInfo,
            currencyForm: state => state.equip.equipData.currencyForm//币种
        }),
    },
    methods: {
        //监听税率
        monitorTaxRate () {
            this.count()
            console.log(11)
        },
        //获取本地登录信息
        getUserInfo () {
            const { baseCyByOrgId } = this.userInfo
            this.basicForm.baseCurName = baseCyByOrgId.currencyName  //本位币
            this.basicForm.baseCurId = baseCyByOrgId.currency
            const userInfo = JSON.parse(window.localStorage.getItem('vuex')).userInfo.orgInfo

            console.log(JSON.parse(window.localStorage.getItem('vuex')).userInfo, '112211')

            this.basicForm.preparer = JSON.parse(window.localStorage.getItem('vuex')).userInfo.userName
            this.basicForm.preparerId = JSON.parse(window.localStorage.getItem('vuex')).userInfo.userId

            this.basicForm.orgId = userInfo.orgId
            this.basicForm.orgName = userInfo.orgName

        },
        //获取币种列表
        //币种切换
        currencyChange (val) {
            this.basicForm.currency = this.currencyForm.find(x=>x.value === val).label  //币种
            if(this.basicForm.currency === '人民币') {
                this.basicForm.rmbRate = 1
            }else{
                getRmbRate({  //获取机构下某币种的人民币汇率
                    currency: val,
                    orgId: this.userInfo.orgInfo.orgId
                }).then(res=>{
                    this.basicForm.rmbRate = res.rate
                }).catch(()=>{

                })
            }
            if(this.basicForm.currencyId === this.basicForm.baseCurId) {   //币种=本位币
                this.basicForm.baseCurRate = 1
                this.showBaseCurName = false
            }else{
                this.basicForm.baseCurRate = null
                this.showBaseCurName = true
            }
        },
        //获取当前日期
        //基本信息保存
        addSave () {

            this.$refs.ruleForm.validate(valid => {
                if (valid) {
                    this.$Ajax
                        .httpPost({
                            url: '/turnover/annualDemandPlan/create',
                            params: this.basicForm
                        })
                        .then(res => {
                            this.clientPop('info', '保存成功!是否进行下一步操作？', ()=>{
                                this.$router.push({
                                    path: '/yearNeedPlanEdit',
                                    query: {
                                        billid: res,
                                        name: 'detailed'
                                    }
                                })
                            },
                            null,
                            this.handleClose
                            )

                        })
                }
            })
        },
        //取消
        handleClose () {
            this.$router.push('/yearNeedPlan')

        }
    }
}
</script>

<style lang="scss" scoped>
.buttons {
    left: 0;
}
.e-form {
    padding: 0 20px;
    .tabs-title::before {
        content: '';
        height: 22px;
        width: 8px;
        border-radius: 40px;
        background-color: #2e61d7;
        display: block;
        position: absolute;
        left: 20px;
        margin-right: 20px;
    }
}
.e-table {
    min-height: auto;
    background: #fff;
}
.separ {
    width: 30px;
    height: 40px;
    line-height: 18px;
    text-align: center;
}
.upload {
    margin: 20px auto;
    display: flex;
    justify-content: center;
    text-align: center;
}

.upload-demo {
    display: flex;
    justify-content: center;
    align-items: center;
}

::v-deep.el-input--suffix .el-input__inner {
    padding-right: 5px;
}
::v-deep.el-table .cell {
    text-align: center;
}
</style>
